package main;

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import ui.MainFrame;

import common.log.WindLog;

public class WindTrayIcon extends TrayIcon implements WindTrayIconNotification
{

	final static int ICON_DEFAULT = 0;
	final static int ICON_ERROR = 1;
	final static int ICON_REFRESH = 2;

	public static Image[] iconTrayImage = {
			Toolkit.getDefaultToolkit().getImage(".\\images\\charts.png"),
			Toolkit.getDefaultToolkit().getImage(".\\images\\close.png"),
			Toolkit.getDefaultToolkit().getImage(".\\images\\CycleV.png") };

	private PopupMenu popup;
	private DataController mdc;
	private MainFrame df;

	private MouseListener mouseListener = new MouseListener() {

		public void mouseClicked(MouseEvent e) {
			if (e.getButton() == MouseEvent.BUTTON1)
				displayMessage("Last update: ", mdc.getMainDataSet()
						.getLastRecord(), TrayIcon.MessageType.INFO);
		}

		public void mouseEntered(MouseEvent e) {
			// System.out.println("Tray Icon - Mouse entered!");
		}

		public void mouseExited(MouseEvent e) {
			// System.out.println("Tray Icon - Mouse exited!");
		}

		public void mousePressed(MouseEvent e) {
			// System.out.println("Tray Icon - Mouse pressed!");
		}

		public void mouseReleased(MouseEvent e) {
			// System.out.println("Tray Icon - Mouse released!");
		}
	};

	public WindTrayIcon(DataController aMainCtr) {
		super(iconTrayImage[0]);
		mdc = aMainCtr;
		init();
		this.setPopupMenu(popup);
	}

	private void init() {
		setpopUpMenu();

		if (mdc.isOK()){
			this.setImage(iconTrayImage[0]);
			df = new MainFrame(mdc.getMainDataSet());
			df.pack();
		}
		else
			this.setImage(iconTrayImage[1]);


		
		this.setToolTip("Wind Data Display");

		this.setImageAutoSize(true);
		// this.addActionListener(actionListener);
		this.addMouseListener(mouseListener);
	}

	private void setpopUpMenu() {
		popup = new PopupMenu();
		MenuItem exitItem = new MenuItem("exit");
		exitItem.addActionListener(exitListener);

		MenuItem refreshItem = new MenuItem("refresh");
		refreshItem.addActionListener(refreshListener);

		MenuItem displayItem = new MenuItem("diplay data");
		displayItem.addActionListener(displayListener);

		popup.add(refreshItem);
		popup.add(displayItem);
		popup.addSeparator();
		popup.add(exitItem);

	}

	ActionListener exitListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			WindLog.getLogger().logInfo("Exiting...");
			System.exit(0);
		}
	};

	ActionListener refreshListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			try {
				setImage(iconTrayImage[2]);
				mdc.refreshDataSet();
				setImage(iconTrayImage[0]);
			} catch (Exception e1) {
				WindLog.getLogger().logSevere(
						"Refreshing data...:" + e1.getMessage());
				setImage(iconTrayImage[1]);
			}
		}
	};

	ActionListener displayListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if(df!=null){
				df.refeshChart();
				df.setVisible(true);
			}	
		}
	};

	@Override
	public void changeIcon(int icon) {
		this.setImage(iconTrayImage[icon]);
	}

}
